<?php

class bases extends spController {

	public function __construct(){
        parent::__construct();
		try{
            if($_SERVER['REQUEST_METHOD'] != 'POST')
                throw new Exception("method error");
            global $sys_config;
           
            if($sys_config["ALIYUN_OSS"]["on"]){
                $this->dataurl = $sys_config["ALIYUN_OSS"]["sourceUrl"];
            }else{
                $is_http = "http://";
                if($this->is_https()){
                    $is_http = "https://";
                }
                $this->dataurl = $is_http.$_SERVER['SERVER_NAME'].URL_ROOT."/";
            }
            if($_POST['uid']){
                $this->login_rs = $this->getUserInfo($_POST["uid"]);
            }
           
		}catch(Exception $e){
            echo json_encode(array("data"=>array(), "message"=>$e->getMessage(), "code"=>500));
            exit();
		}
	}

	private function is_https() {
        if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
            return true;
        } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }
        return false;
    }

    public function returnApi($redata, $relogin = array()){
        $login_rs = $this->login_rs;
        if($relogin['id']){
            $login_rs = $this->getUserInfo($relogin["id"]);
            $login_rs['student_info'] = $this->getStudentInfo($relogin["id"]);
        }
        
        if($login_rs){
            $redata["userInfo"] = $login_rs;
            $redata["userInfo"]["islogin"] = 1;
        }else{
            $redata["userInfo"]["islogin"] = 0;
        }
        echo json_encode(array("data"=>$redata, "message"=>"success", "code"=>"1"));
        exit();
    }

    protected function getUserInfo($uid){
        try{
            if(!$uid)
                throw new Exception("用户id丢失");
            $obj_student = spClass('students');
            $student_info = $obj_student -> find(array('id' =>$uid));
            if(!$student_info){
                throw new Exception("获取用户信息出错！");
            }
            return $student_info;
        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(), "code"=>"-1"));
            exit();
        }
    }

    protected function getStudentInfo($uid){
        try{
            if(!$uid)
                throw new Exception("用户id丢失");
            $obj_student_info = spClass('student_info');
            $student_info = $obj_student_info->join('sys_classes','sys_classes.id = sys_student_info.grade_id','left')->find(array("sys_student_info.uid" => $uid),null,'sys_student_info.*, sys_classes.name as grade_name');
            return $student_info;

        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(), "code"=>"-1"));
            exit();
        }
    }

}
?>